package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceParam;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;
    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        log.debug("查询围栏业务:geofenceQuery={}",geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.selectGeofence(geofenceQuery);

        //之前没有对围栏的车辆信息进行处理，现在需要进行业务补充:
        /*需要遍历每一个围栏集合*/
        for (int i = 0;i<list.size();i++) {
            GeofenceVO geofenceVO = list.get(i);
            //设置封装车辆查询对象
            VehicleQuery vehicleQuery = new VehicleQuery();
            //车辆查询条件需要封装当前遍历到围栏id,也就是查询绑定了指定围栏的所有车辆
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
            //获取给围栏上绑定的车辆总数:车辆列表size
            int totalNum = vehicleVOList.size();
           // 定义用来保存围栏上的可用车辆数
            int availableNum = 0;
            //遍历车辆列表，判断车辆状态，如果状态为1，则表示车辆可用，可用车辆数+1
            for(VehicleVO vehicleVO:vehicleVOList){
                if (vehicleVO.getStatus().equals("1")){
                    availableNum++;
                }
            }
            geofenceVO.setTotalNum(totalNum);
            geofenceVO.setAvailableNum(availableNum);
            geofenceVO.setVehicleList(vehicleVOList);
        }
        return list;
    }

    @Override
    public void savaGeofence(GeofenceParam geofenceParam) {
        log.debug("保存围栏业务:geofenceParam={}",geofenceParam);
        Geofence geofence =new Geofence();
        BeanUtils.copyProperties(geofenceParam,geofence);
        if (geofence.getId() != null){
            geofence.setUpdateTime(new Date());
            geofenceMapper.update(geofence);
        }else {
            geofence.setStatus("1");
            geofence.setCreateTime(new Date());
            geofenceMapper.insert(geofence);
        }
    }

    @Override
    public void deleteGeofence(Long id) {
        log.debug("删除围栏业务:id={}",id);
        //判断该围栏是否有绑定车辆
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        if (list.size()>0 && list != null){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }else {
            geofenceMapper.deleteById(id);
        }
        ;
    }
}
